package com.jerry.huihome.module.register.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.OnSendMessageHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by Administrator on 2016/5/16.
 */
public class RegisterActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.bt_get_codes)
    Button btGetCodes;
    @BindView(R.id.et_codes)
    EditText etCodes;
    @BindView(R.id.bt_next)
    Button btNext;
    private String phoneNum;
    private EventHandler eh;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_me_register_ui;
    }

    @Override
    protected void initViews() {

        SMSSDK.registerEventHandler(new EventHandler(){
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成
                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码成功
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            toast("验证成功");
                            goPersonalInfo();
                        } else {
                            toast("验证失败");
                        }
                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        //获取验证码成功
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            toast("获取验证码成功");
                            //默认的智能验证是开启的,我已经在后台关闭
                        } else {
                            toast("获取验证码失败");
                        }

                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表
                    }
                }
                else
                {
                    ((Throwable)data).printStackTrace();
                }
            }
        });
    }
    private void toast(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void goPersonalInfo() {
        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,PersonalInformationActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.bt_get_codes, R.id.bt_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_get_codes:
                phoneNum = etPhone.getText().toString();
                SMSSDK.getVerificationCode("86", phoneNum, new OnSendMessageHandler() {
                    @Override
                    public boolean onSendMessage(String s, String s1) {
                        return false;
                    }
                });
                break;
            case R.id.bt_next:
                String codes = etCodes.getText().toString();
                SMSSDK.submitVerificationCode("86",phoneNum,codes);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        SMSSDK.unregisterAllEventHandler();
    }
}
